import { defineStore } from "pinia";
import { setLocalStorage, getLocalStorage } from '@/utils';

export const useTableColumnsStore = defineStore({
    id: 'tableColumnsStore',
    state: () => {
        // 尝试从 LocalStorage 加载用户列设置
        const savedState = getLocalStorage('tableColumnsStore');
        return  {
            userColumns: savedState ? savedState : {},
        };
    },
    actions: {
        // 保存用户的列设置到 store 和 LocalStorage
        saveUserColumns(userId: string, tableId: string, columns: string[]) {
            if (!this.userColumns[userId]) {
                this.userColumns[userId] = {};
            }
            this.userColumns[userId][tableId] = columns;
            // 更新 LocalStorage
            setLocalStorage('tableColumnsStore', this.userColumns);
        },
        // 获取用户的列设置，从 store 中获取
        getUserColumns(userId: string, tableId: string): string[] {
            return this.userColumns[userId]?.[tableId] || [];
        },
    },
})

